package com.example.chat.service.impl;

import com.example.chat.constant.ErrorMessageConstant;
import com.example.chat.model.Role;
import com.example.chat.model.RolePermission;
import com.example.chat.model.ro.RoleRO;
import com.example.chat.repository.RolePermissionRepository;
import com.example.chat.repository.RoleRepository;
import com.example.chat.service.InitService;
import com.example.chat.service.RoleService;
import com.example.chat.utils.ChatException;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Date 2022/11/17 10:11
 * @Author pq
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private InitService initService;

    @Resource
    private RoleRepository roleRepository;

    @Resource
    private RolePermissionRepository rolePermissionRepository;

    @Override
    public void updateRole(RoleRO ro) {
        if (initService.getRoleIds().contains(ro.getRoleId())) {
            throw new ChatException(ErrorMessageConstant.INITIAL_ROLE_COULD_NOT_EDIT);
        }
        roleRepository.findByIdAndDeletedIsFalse(ro.getRoleId()).ifPresentOrElse(
                role -> {
                    roleRepository.findByNameAndDeletedIsFalse(ro.getRoleName()).ifPresent(r -> {
                        throw new ChatException(ErrorMessageConstant.ROLE_ALREADY_EXISTED);
                    });
                    List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdAndDeletedIsFalse(ro.getRoleId());
                    rolePermissions.forEach(rolePermission -> {
                        rolePermission.setDeleted(true);
                        rolePermission.setModifyDate(new Date());
                        rolePermission.setModifyUser(ro.getCurrentUser());
                    });
                },
                () -> {
                    roleRepository.findByNameAndDeletedIsFalse(ro.getRoleName()).ifPresent(role -> {
                        throw new ChatException(ErrorMessageConstant.ROLE_ALREADY_EXISTED);
                    });
                    Role newRole=new Role();
                    newRole.setId(ro.getRoleId());
                    newRole.setName(ro.getRoleName());
                    roleRepository.save(newRole);
                    List<RolePermission> rolePermissions= Lists.newArrayList();
                    ro.getPermissionId().forEach(permissionId->{
                        RolePermission rolePermission=new RolePermission();
                        rolePermission.setRoleId(ro.getRoleId());
                        rolePermission.setPermissionId(permissionId);
                        rolePermissions.add(rolePermission);
                    });
                    rolePermissionRepository.saveAll(rolePermissions);
                }
        );

    }
}
